package com.jlect.swebing.renderers.gwt.client;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.jlect.swebing.renderers.client.CheckboxRenderer;
import com.jlect.swebing.renderers.client.ClickActionEvent;
import com.jlect.swebing.renderers.client.RendererActionListener;

/**
 * Checkbox renderer GWT based implementation
 *
 * @author Sergey Kozmin
 * @since 18.11.2007 13:53:44
 */
public class CheckboxRendererGWTImpl extends AbstractGWTRenderer implements CheckboxRenderer, ClickListener {
    private RendererActionListener clickListener;
    private ClickActionEvent event;

    private CheckBox getCheckBoxWidget() {
        return (CheckBox) ((CompositeAttachEventProvider)getWidget()).getTargetWidget();
    }

    protected Widget provideWidget() {
        CheckBox targetWidget = new CheckBox();
        targetWidget.addClickListener(this);
        return new CompositeAttachEventProvider(targetWidget);
    }

    public void setText(String text) {
        getCheckBoxWidget().setText(text);
    }

    public void setChecked(boolean checked) {
        getCheckBoxWidget().setChecked(checked);
    }

    public boolean getChecked() {
        return getCheckBoxWidget().isChecked();
    }

    public void setActionListener(RendererActionListener listener) {
        clickListener = listener;
    }

    public void setEnabled(boolean enabled) {
        getCheckBoxWidget().setEnabled(enabled);
    }

    public void onClick(Widget widget) {
        if(clickListener != null) {
            if(event == null) {
                event = new ClickActionEvent(this, 0);//todo implement modifiers
            }
            clickListener.fireActionPerformed(event);
        }
    }
}